package com.zwz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwz.bean.AirlineInfo;
import com.zwz.controller.util.Result;
import com.zwz.service.AirlineInfoService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author Alysia
 * @date 2022/11/10 10:00
 * @description 航班控制器类
 */

@RestController
@RequestMapping("/airline")
public class AirlineController {

    @Resource
    private AirlineInfoService airlineInfoService;

    /**
     * 航班直达查询
     *
     * @param airlineInfo 起止城市
     * @return 航班信息
     */
    @GetMapping("/direct")
    public Result queryDirect(AirlineInfo airlineInfo) {
        System.out.println(airlineInfo);
        QueryWrapper<AirlineInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("begin_city", airlineInfo.getBeginCity());
        queryWrapper.eq("last_city", airlineInfo.getLastCity());
        return Result.success(airlineInfoService.list(queryWrapper));

    }

    /**
     * 航班中转查询（目前只能查询中转一次）
     *
     * @param airlineInfo 起止城市
     * @return 航班信息
     */
    @GetMapping("/transit")
    public Result queryTransit(AirlineInfo airlineInfo) {
        return Result.success(airlineInfoService.queryTransit(airlineInfo));

    }

    /**
     * 添加航线
     *
     * @param airlineInfo 航线信息
     * @return 成功标志
     */
    @PostMapping("/add")
    public Result addAirline(@RequestBody AirlineInfo airlineInfo) {
        if (airlineInfoService.save(airlineInfo)) {
            return Result.success("添加成功！");
        } else {
            return Result.error("服务器异常！");
        }
    }

    /**
     * 分页查询相关
     *
     * @param currentPage 当前页码
     * @param pageSize    每页条数
     * @param airlineInfo 航线的信息
     * @return 一页评论信息数据
     */
    @GetMapping("/page/{currentPage}/{pageSize}")
    public Result page(@PathVariable int currentPage, @PathVariable int pageSize, AirlineInfo airlineInfo) {
        IPage<AirlineInfo> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<AirlineInfo> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(airlineInfo.getBeginCity()), AirlineInfo::getBeginCity, airlineInfo.getBeginCity());
        lqw.eq(Strings.isNotEmpty(airlineInfo.getLastCity()), AirlineInfo::getLastCity, airlineInfo.getLastCity());
        lqw.orderByDesc(AirlineInfo::getAirlineId);
        return Result.success(airlineInfoService.page(page, lqw));
    }

    /**
     * 删除一条航班信息
     *
     * @param airlineId 航班id
     * @return 成功标志
     */
    @DeleteMapping("/delete/{airlineId}")
    public Result delete(@PathVariable Integer airlineId) {
        if (airlineInfoService.removeById(airlineId)) {
            return Result.success("删除成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 修改航班信息
     *
     * @param airlineInfo 修改后的航班信息
     * @return 成功标志
     */
    @PutMapping("/update")
    public Result update(@RequestBody AirlineInfo airlineInfo) {
        if (airlineInfoService.updateById(airlineInfo)) {
            return Result.success("修改成功！");
        } else {
            return Result.error("服务器错误！");
        }
    }

    /**
     * 展示某一条数据
     *
     * @param airlineId 航班信息id
     * @return 一条航班信息数据
     */
    @GetMapping("/show/{airlineId}")
    public Result show(@PathVariable Integer airlineId) {
        AirlineInfo airlineInfo = airlineInfoService.getById(airlineId);
        return Result.success(airlineInfo);
    }
}
